﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;

namespace RAP.Framework.Libary.Utils
{
    /// <summary>
    /// 数字（Int32、Float、Double）对象扩展
    /// </summary>
    public static partial class Extensions
    {
        /// <summary>
        /// 获取指定字节数字对应的文件尺寸字符串（将字节数转换成合适单位的尺寸：字节、KB、MB、GB、TB）
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static string ToFileSize ( this int size )
        {
            var kb = 1024;
            var mb = kb * kb;
            long gb = kb * mb;
            long tb = kb * gb;

            if ( size < kb )
            {
                return $"{size}字节";
            }
            if ( size < mb )
            {
                return $"{size / kb}KB";
            }
            if ( size < gb )
            {
                return $"{size / ( mb )}M";
            }
            if ( size < tb )
            {
                return $"{size / gb }G";
            }
            return $"{size / tb}T";
        }

        /// <summary>
        /// 将数字转换成货币字符串：100=>100、100.01=>100.01、0.01=>0.01
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string ToMoney ( this decimal num )
        {
            if ( num == ( int )num )
            {
                return num.ToString( "0" );
            }

            return num.ToString( "0.00" );
        }
    }
}
